﻿namespace FallingRocks
{
    public class Dwarf : GameObject
    {
        public Dwarf(Position topLeft, char[] visualisation, Color color, int lives, int score)
            : base(topLeft, visualisation, new Position(0, 0), color)
        {
            this.Lives = lives;
            this.Score = score;
        }

        public int Lives { get; protected set; }

        public int BonusesObtained { get; protected set; }

        public int Score { get; protected set; }

        public void MoveLeft()
        {
            if (this.Position.X > 0)
            {
                this.Position -= new Position(1, 0);
            }            
        }

        public void MoveRight(int end)
        {
            if (this.Position.X + this.Visualisation.Length < end - 2)
            {
                this.Position += new Position(1, 0);
            }            
        }

        public override bool CanCollideWith(GameObject gameObject)
        {
            return gameObject.GetType() == typeof(Rock) || gameObject.GetType() == typeof(SpecialObject);
        }

        public override void Update()
        {
            this.IncreaseScore(1);
        }

        public override bool RespondToCollision(GameObject gameObject)
        {
            if (gameObject is Rock)
            {
                if (this.BonusesObtained == 0)
                {
                    this.Lives--;
                    return true;
                }
                else
                {
                    this.BonusesObtained--;
                    return false;
                }                
            }
            else if (gameObject is SpecialObject)
            {
                this.BonusesObtained += 3;
                this.IncreaseScore(500);

                return false;
            }
            else
            {
                return false;
            }
        }

        private void IncreaseScore(int amount)
        {
            this.Score += amount;
        }
    }
}
